<?php


namespace App\Http\Services;


use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class NotifyService
{
    /**
     * 创建公告
     * @param $content
     * @param $sender
     */
    public function createAnnounce($content, $sender)
    {
        DB::table('notify')->insert([
            'content' => $content,
            'type' => 1,
            'target_type' => 'member',
            'sender' => $sender,
            'created_at' => Carbon::now()
        ]);
    }

    /**
     * 创建提醒
     * @param $target
     * @param $targetType
     * @param $action
     * @param $sender
     * @param $content
     */
    public function createRemind($target, $targetType, $action, $sender, $content)
    {
        try {
            DB::beginTransaction();
            $time = Carbon::now();
            $notifyId = DB::table('notify')->insertGetId([
                'type' => 2,
                'target' => $target,
                'target_type' => $targetType,
                'action' => $action,
                'sender' => $sender,
                'content' => $content,
                'created_at' => $time
            ]);
            DB::table('member_notify')->insert([
                'is_read' => 0,
                'member_id' => $target,
                'notify_id' => $notifyId,
                'created_at' => $time
            ]);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
        }
    }

    /**
     * 往notify插入一条信息
     * 往member_notify插入一条信息
     * @param $content
     * @param $sender
     * @param $receiver
     */
    public function createMessage($content, $sender, $receiver)
    {
        try {
            DB::beginTransaction();
            $time = Carbon::now();
            $notifyId = DB::table('notify')->insertGetId([
                'content' => $content,
                'type' => 3,
                'target' => $receiver,
                'target_type' => 'member',
                'sender' => $sender,
                'created_at' => $time
            ]);
            DB::table('member_notify')->insert([
                'is_read' => 0,
                'member_id' => $receiver,
                'notify_id' => $notifyId,
                'created_at' => $time
            ]);

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
        }
    }

    /**
     * 从UserNotify中获取最近的一条公告信息的创建时间: lastTime
     * 用lastTime作为过滤条件，查询Notify的公告信息
     * 新建UserNotify并关联查询出来的公告信息
     * @param $member
     */
    public function pullAnnounce($member)
    {
        $last = DB::table('member_notify')->where('member_id', $member)->latest()->first();
        $notifies = DB::table('notify')->where('created_at', '>', $last->created_at)->get();
        $data = [];
        foreach ($notifies as $notify) {
            $data[] = [
                'is_read' => 0,
                'member_id' => $member,
                'notify_id' => $notify->id,
                'created_at' => $notify->created_at
            ];
        }
        DB::table('member_notify')->insert($data);
    }

    /**
     * 查询用户的订阅表，得到用户的一系列订阅记录
     * 通过每一条的订阅记录的target、targetType、action、createdAt去查询Notify表，获取订阅的Notify记录。（注意订阅时间必须早于提醒创建时间）
     * 查询用户的配置文件SubscriptionConfig，如果没有则使用默认的配置DefaultSubscriptionConfig
     * 使用订阅配置，过滤查询出来的Notify
     * 使用过滤好的Notify作为关联新建UserNotify
     * @param $member
     */
    public function pullRemind($member)
    {
        $last = DB::table('member_notify')->where('member_id', $member)->latest()->first();
        $notifies = DB::table('notify')->where('id', '>', $last->id)->get();
    }

    /**
     * 获取用户消息列表
     * @param $member
     */
    public function getUserNotify($member, $action)
    {
        return DB::table('member_notify as m')->select('m.is_read','n.content','n.action', 'n.created_at')->leftJoin('notify as n', 'n.id', '=', 'm.notify_id')
            ->orderByDesc('m.created_at')
            ->where('m.member_id', $member)
            ->when($action, function ($query, $action) {
                $query->where('n.action', $action);
            })
            ->get();
    }

    /**
     * 标记已读
     * @param $member
     * @param $notify_id
     */
    public function read($member, $notify_id)
    {

    }
}